/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.controller.assembler;

import com.google.common.collect.Lists;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.permit.controller.model.dto.RolePermitCountDto;
import net.siufung.market.base.permit.controller.model.vo.RoleVo;
import net.siufung.market.base.permit.mapper.entity.Role;
import net.siufung.market.base.permit.spi.dto.RoleUserCountDto;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 *
 * 系统角色表 持久层展示层组装器
 *
 * @author 陈建峰
 * @since 2022-07-05 16:14:50
 */
public class RoleAssembler {

    /**
     * Role 转换成 RoleVo
     * @param role 数据库实体
     * @return RoleVo 视图
     */
    public static RoleVo from(Role role){
        if(!Objects.isNull(role)){
            RoleVo roleVo = new RoleVo();
            CopyUtil.copyProperties(role, roleVo);
            return roleVo;
        }
        return null;
    }

    /**
     * RoleList 转换成 RoleVoList
     * @param roleList 数据库实体集合
     * @return List<RoleVo> 视图集合
     */
    public static List<RoleVo> fromList(List<Role> roleList){
        List<RoleVo> roleVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(roleList)){
            RoleVo roleVo;
            for (Role role: roleList) {
                roleVo = new RoleVo();
                CopyUtil.copyProperties(role, roleVo);
                roleVoList.add(roleVo);
            }
        }
        return roleVoList;
    }

    /**
     * RoleList 转换成 RoleVoList
     * @param roleList 数据库实体集合
     * @param userCountDtoList 数据库实体集合
     * @param rolePermitCountDtoList 权限集合
     * @return List<RoleVo> 视图集合
     */
    public static List<RoleVo> fromList(List<Role> roleList, List<RoleUserCountDto> userCountDtoList,
                                        List<RolePermitCountDto> rolePermitCountDtoList){
        List<RoleVo> roleVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(roleList)){
            RoleVo roleVo;
            for (Role role: roleList) {
                roleVo = new RoleVo();
                CopyUtil.copyProperties(role, roleVo);
                for (RoleUserCountDto roleUserCountDto: userCountDtoList) {
                    if(roleVo.getId().intValue() == roleUserCountDto.getId().intValue()){
                        roleVo.setUserCount(roleUserCountDto.getUserCount());
                        break;
                    }
                }
                for (RolePermitCountDto rolePermitCountDto: rolePermitCountDtoList) {
                    if(roleVo.getId().intValue() == rolePermitCountDto.getId().intValue()){
                        roleVo.setPermitCount(rolePermitCountDto.getPermitCount());
                        break;
                    }
                }
                roleVoList.add(roleVo);
            }
        }
        return roleVoList;
    }
}

